//导入题库的数据模型

const { T_exam_paper_info } = require('../models/t_exam_paper_info')



//传输subid查找标签名
exports.getALLPaperInfo = async (req, res) => {
    const ACSId = req.query.ACSId;
    const type = req.query.paperType;
    if (ACSId && type) {
        try {
            const T_info = await T_exam_paper_info.findOne({
                where: {
                    t_user_and_a_c_s_id: ACSId,
                    type: type
                }

            });//根据id查询
            if (T_info) {
                res.json({
                    "code": 200,
                    "msg": "查询成功",
                    "data": T_info,
                });
            } else {
                res.json({
                    "code": 404,
                    "mag": "查询失败,该题库中的题目id不存在",
                })
            };
        } catch (error) {
            // console.log(error.message);
            res.json({
                "code": 500,
                "msg": error
            });
        }
    }

};



//插入标签
exports.createt_paperInfo = async (req, res) => {
    const body = req.body;


    try {
        const judge = await T_exam_paper_info.findOne({
            where: {
                type: body.type,
                t_user_and_a_c_s_id: body.ACSId,
            }
        })

        if (judge) {
            const T_info = await T_exam_paper_info.create({
                order: body.order,
                type: body.type,
                t_user_and_a_c_s_id: body.ACSId,
            });
            return res.json({
                "code": 1,
                "msg": '创建成功',
                "data": T_info,
            });
        }

        return res.json({
            "code": 1,
            "msg": '重复插入',
            // "data": T_info,
        });

    } catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        });
    }
};

//更新题库中的标签信息
exports.update_PaperInfo = async (req, res) => {
    const body = req.body

    try {

        await T_exam_paper_info.update(
            {
                order: body.order,
                type: body.type
            },
            {
                where: {
                    id: body.id,
                }
            },
        );

        res.json({
            "code": 1,
            "msg": "更新成功",
        });

    }
    catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        })
    }
}

//删除顺序
exports.delete_paperInfo = async (req, res) => {
    const id = req.query.id;
    console.log(id);
    try {
        const deletedRows = await T_exam_paper_info.destroy({
            where: { id },
        });
        if (deletedRows > 0) {
            res.json({
                "code": 1,
                "msg": '删除成功',
            });
        } else {
            res.json({
                "code": 404,
                "msg": `不存在该科目`,
            });
        }
    } catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        });
    }
};